<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Image;
class AuditController extends PublicController {


	function _initialize() {
		parent::_initialize ();
	}
	//商务列表
    public function index(){
        $search = I('search');
        if($search){
            $where['user_name'] = array('like',"%$search%");
            $this->assign("search", $search);
        }

        if(I('p')){
            $p = I('p');
        }else{
             $p = 1;
        }
        $header = C('header')['audit'];
        $where['q.status'] = 0;
        $count =    M('query')
                    ->alias("q")
                    ->join("left join pt_admin as a on a.admin_id=q.admin_id")
                    ->join("left join pt_user as u on u.user_id=q.user_id")
                    ->field('q.id,a.user_name as adminName,u.user_name as userName,q.kwd,q.logtime')
                    ->where($where)
                    ->count();



        
        $Page = new \Think\Page($count, 30);
        $Page->setConfig('theme', "%UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show = $Page->show(); // 分页显示输出





        $list = M('query')
                ->alias("q")
                ->join("left join pt_admin as a on a.admin_id=q.admin_id")
                ->join("left join pt_user as u on u.user_id=q.user_id")
                ->join("left join pt_site as s on s.query_id=q.id")
                ->where($where)
                ->field('q.id,a.user_name as adminName,u.user_name as userName,q.kwd,q.logtime,q.status')
                ->order('q.id desc')
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();
        $status = ['未审核','通过','未通过'];
        foreach ($list as $k => $v) {
            $list[$k]['logtime'] = date('Y-m-d H:i:s',$v['logtime']);
            $list[$k]['order'] = "<a href='#' class='details' query_id='{$v['id']}'>订单详情</a>";
            $list[$k]['status'] = $status[$v['status']];
        }


        $show = str_replace('<div>','',$show);
        $show = str_replace('current','active',$show);
        $show = str_replace('<<<','上一页<',$show);
        $show = str_replace('>>>','>下一页',$show);
        $show = str_replace('</div>','',$show);
        // echo  htmlentities($show,ENT_QUOTES,"UTF-8");exit;
        // $show = preg_replace('//s*/', '', $show);
        if($count <= 10){
            $show = '<a href="" class="active">1</a>';
        }
        $this->assign("page", $show); // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("p", $p);
        $this->assign("header", $header);
        $this->assign("list", $list);
        layout('Layout/twolayout');
        $this->display();
    }


     public function query(){
        $query_id = I('query_id');
        if(!$query_id){
            $this->ajaxreturn(array('state' => 0, 'data' => '信息错误'), 'json');
        }
        $where['query_id'] = $query_id;
        
        $list = M('site')->where($where)->field('site,ftp_ip,username,adminadd,adminusername,adminpassword,password,seo')->find();
        if(empty($list)){
            $this->ajaxreturn(array('state' => 0, 'data' => '客户没有提交订单'), 'json');
        }else{
            $seo = json_decode($list['seo']);
            unset($list['seo']);
            $this->ajaxreturn(array('state' => 1, 'data' => $list,'seo'=>$seo), 'json');
        }
    }


    //商务编辑
    public function handle(){
        $res = I('');
        if(empty($res['act']) || empty($res['id'])){
             $this->error('请填写完整信息');exit;
        }
        $where['id'] = $res['id'];
        $update['status'] = $res['act'];
        $list = M('query')->where($where)->save();
        if($list){
            $this->success("更新成功", U("Audit/index"));exit;
        }else{
            $this->error("更新失败");exit;
        } 
    }




    //官网推首页
    public function ptindex(){
        layout('Layout/twolayout');
        $this->display();
    }

    public function pttable(){
        // $header = ['用户名','公司名称','电话','邮箱','更新时间'];
        $header = C('header')['BusinessList'];
        $where['type'] = 1;
        $list = M('admin')->where($where)->field('user_name,company_name,phone,email,add_time')->select();
        foreach ($list as $k => $v) {
            $list[$k]['add_time'] = date('Y-m-d',$v['add_time']);
        }
        $this->assign("header", $header);
        $this->assign("list", $list);
        layout('Layout/twolayout');
        $this->display();
    }


    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =      ''; // 设置附件上传（子）目录
        // 上传文件 
        $info   =   $upload->upload();

        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功 获取上传文件信息
            foreach($info as $file){
                $files[$file['key']] =  '/Uploads/'.$file['savepath'].$file['savename'];
            }
        }

        return $files;
    }
}